<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\base\BaseController;
use app\home\validate\EnterpriseCheck;
use app\home\validate\ItemCheck;
use app\home\validate\MonthTimeCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class MonthTime extends BaseController
{
    /**
     * 月工时查询
     */
    public function index()
    {
        $param = get_params();

        $eid = !empty($param['eid']) ? $param['eid']: $this->eid;

        $year = !empty($param['year']) ? $param['year']: date('Y');

        $deparment = Db::name('Enterprise')->where(['id'=>$eid])->find();

        view::assign('date' , $year);
        if (request()->isAjax()) {

            $where = array();

            $where['eid'] = $eid;
            $where['year'] = $year;

            $content = Db::name('MonthTime')
                ->order('month asc')
                ->where($where)
                ->select()->toArray();

            $list = [];
            $month_value = [];
            if(!empty($content)){
                $month_value = array_column($content , Null , 'month');
            }

            for($i=1;$i<=12;$i++){
                if(isset($month_value[$i]) && !empty($month_value[$i])){
                    $list[$i] = $month_value[$i];
                }else{
                    $list[$i] = [
                        'id'        => 0,
                        'year'      => $year,
                        'month'     => $i,
                        'work_hours'     => 0,
                    ];
                }
            }

            array_walk($list, function(&$item) use ($deparment){
                $item['d_name'] = $deparment['title'] ??'系统管理员';
            });

            $arr['data'] = $list;

            return table_assign(0, '', $arr);
        } else {

            return view();
        }
    }

    /**
     * 添加/编辑项目项目
     */
    public function add()
    {
        $param = get_params();

        if(empty($param['eid'])){
            $param['eid'] = $this->eid;
        }

        if (!empty($param['id']) && $param['id'] > 0) {
            try {
                validate(MonthTimeCheck::class)->scene('edit')->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
            $param['update_time'] = time();
            $res = Db::name('MonthTime')->strict(false)->field(true)->update($param);
            if ($res) {
                add_log('edit', $param['id'], $param);
            }
            return to_assign();
        } else {
            try {
                validate(MonthTimeCheck::class)->scene('add')->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
            $param['create_time'] = time();
            $insertId = Db::name('MonthTime')->strict(false)->field(true)->insertGetId($param);
            if ($insertId) {
                add_log('add', $insertId, $param);
            }
            return to_assign();
        }

    }


    public function getMonthTime(){

        $param = get_params();

        $eid = !empty($param['eid']) ? $param['eid']: $this->eid;

        $year = !empty($param['year']) ? $param['year']: date('Y');

        $month = !empty($param['year']) ? $param['year']: date('m');

        $where['eid'] = $eid;
        $where['year'] = $year;
        $where['month'] = $month;

        $content = Db::name('MonthTime')
            ->order('month asc')
            ->where($where)
            ->find();

        return to_assign(0, '', $content);

    }


}
